package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.JqzzSetting;
import com.jy.adscm.operation.mscyb.bean.QueryJqbzSettings;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqzzSettingMapper;
import com.jy.adscm.operation.mscyb.service.JqzzSettingService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.JyDrawingType;
import com.jy.adscm.zltx.mapper.mysql.JyDrawingTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-05-21
 */
@Service
public class JqzzSettingServiceImpl extends ServiceImpl<JqzzSettingMapper, JqzzSetting> implements JqzzSettingService {
     @Autowired
    JyUserMapper jyUserMapper;
     @Autowired
    JyDrawingTypeMapper jyDrawingTypeMapper;
    @Override
    public JqzzSetting selectJqzzSettingById(String id) throws AdscmSystemException {
        JqzzSetting jqzzSetting = super.selectById(id);
        if (AdscmBaseUtils.getInstance().isNotEmpty(jqzzSetting,null)){
            JyUser jyUser = jyUserMapper.selectById(jqzzSetting.getCreator());
            if(AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                jqzzSetting.setCreatorName(jyUser.getName());
            }else {
                jqzzSetting.setCreatorName(jqzzSetting.getCreator());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqzzSetting.getType(),null)){
                JyDrawingType jyDrawingType = jyDrawingTypeMapper.selectById(jqzzSetting.getType());
                jqzzSetting.setTypeName(jyDrawingType.getType());
            }
        }

        return jqzzSetting;
    }

    @Override
    public Page<JqzzSetting> selectAll(Page<JqzzSetting> page, QueryJqbzSettings queryJqbzSettings) throws AdscmSystemException {
        Wrapper<JqzzSetting> wrapper=new EntityWrapper<>();
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqbzSettings.getType(),null)){
            wrapper.eq("type",queryJqbzSettings.getType());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqbzSettings.getName(),null)){
            wrapper.like("name",queryJqbzSettings.getName());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqbzSettings.getCode(),null)){
            wrapper.like("code",queryJqbzSettings.getCode());
        }
        wrapper.orderBy("rank",true);
        page= super.selectPage(page,wrapper);
        List<JqzzSetting> jqzzSettings = page.getRecords();
        for (JqzzSetting jqzzSetting : jqzzSettings) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqzzSetting.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(jqzzSetting.getCreator());
                if(AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    jqzzSetting.setCreatorName(jyUser.getName());
                }else {
                    jqzzSetting.setCreatorName(jqzzSetting.getCreator());
                }
            }
            if(AdscmBaseUtils.getInstance().isNotEmpty(jqzzSetting.getPerformer(),null)){
                JyUser jyUser = jyUserMapper.selectById(jqzzSetting.getPerformer());
                jqzzSetting.setPerformerName(jyUser.getName());
            }
          if (AdscmBaseUtils.getInstance().isNotEmpty(jqzzSetting.getType(),null)){
              JyDrawingType jyDrawingType = jyDrawingTypeMapper.selectById(jqzzSetting.getType());
              jqzzSetting.setTypeName(jyDrawingType.getType());
          }

        }
         page.setTotal(selectCount(wrapper));
        return page;
    }
}
